//
//  ClassifyViewController.m
//  share_business
//
//  Created by 吴于凌 on 2017/9/5.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "ClassifyViewController.h"
#import "ClassifyViewCell.h"
#import "GoodsListViewController.h"
const static int tableViewWidth = 100 ;

@interface ClassifyViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UITableView *tableView ;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout ;

@end

@implementation ClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initTableView];
    [self initRightView];
    [self initCollectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setTopTitleView];
    [self topRightItemButton:ImageNamed(@"home_message") image2:nil title1:@"消息" title2:nil];
}

//-(void)setTopTitleView{
//    UIView *titleViewM = [[UIView alloc]initWithFrame:CGRectMake(0, 27, APPWIDTH - 72*2, 35)];
//    UITextField *searchBar = [[UITextField alloc] init];
//    searchBar.delegate = self;
//    searchBar.returnKeyType = UIReturnKeySearch;
//    searchBar.frame = CGRectMake(-60, 0, APPWIDTH - 72, 35);
//    searchBar.placeholder = @"  搜索全部";
//    searchBar.font =[UIFont systemFontOfSize:14];
//    searchBar.backgroundColor = [UIColor colorWithHexString:COLOR_LIGHT_GRAY];
//    [searchBar setBorder:0 cornerRadius:4];
//    [titleViewM addSubview:searchBar];
//    self.tabBarController.navigationItem.titleView = titleViewM;
//}
-(void)setTopTitleView{
    UIView *titleViewM = [[UIView alloc]initWithFrame:CGRectMake(0, 27, APPWIDTH - 72*2, 35)];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(-50, 0, APPWIDTH - 72, 35)];
    
    UIImageView *searchView = [BSWidget commonImageViewWithFrame:CGRectMake(10, 0, 17, 18) image:ImageNamed(@"home_search_tip")];
    searchView.centerY = 35 / 2 ;
    
    UITextField *searchBar = [[UITextField alloc] init];
    searchBar.delegate = self;
    searchBar.returnKeyType = UIReturnKeySearch;
    searchBar.frame = CGRectMake(searchView.maxX +3, 0, APPWIDTH - 72*2, 35);
    searchBar.placeholder = @" 搜索全部";
    searchBar.font =[UIFont systemFontOfSize:14];
    searchBar.backgroundColor = [UIColor clearColor];
    [bgView setBorder:0 cornerRadius:4];
    
    bgView.backgroundColor= [UIColor colorWithHexString:COLOR_LIGHT_GRAY];
    [bgView addSubview:searchBar];
    [bgView addSubview:searchView];
    
    [titleViewM addSubview:bgView];
    self.navigationItem.titleView = titleViewM;
    
}

// 获得焦点
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    GoodsListViewController *vc  = [[GoodsListViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES ;
    [self.navigationController pushViewController:vc animated:NO];
    return YES;
}
-(void)initTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, tableViewWidth, APPHEIGHT ) style:UITableViewStyleGrouped];
    self.tableView.delegate = self ;
    self.tableView.dataSource = self ;
    //边距
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
    //  tableview不显示没内容的cell
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor whiteColor];
    //滑动隐藏键盘
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setBorder:1 cornerRadius:0];
    [self.view addSubview:self.tableView];
}
#pragma mark tableview事件
//每项数据个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10 ;
}
//每项数据高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  50 ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01 ;
}
//一共多少项
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1 ;
}
//每一个cell视图
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString* cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        // 显示最右边的箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.textLabel.text  = [NSString stringWithFormat:@"分类 %ld",indexPath.row] ;
    
    return cell ;
}

#pragma mark CollectionView
-(void)initCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(tableViewWidth, 110, APPWIDTH - tableViewWidth, APPHEIGHT-110) collectionViewLayout:self.flowLayout];
    _collectionView.showsVerticalScrollIndicator =  NO;
    //定义每个UICollectionView 横向的间距
    self.flowLayout.minimumLineSpacing = 0;
    //定义每个UICollectionView 纵向的间距
    self.flowLayout.minimumInteritemSpacing = 0;
    //定义每个UICollectionView 的边距距
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, -0.25, 0, -0.25);//上左下右
    //设置代理
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //背景颜色
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[ClassifyViewCell class] forCellWithReuseIdentifier:@"ClassifyViewCell"];
    //自适应大小
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.collectionView];
}

#pragma mark UICollectionView相关
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 25;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return  CGSizeMake((APPWIDTH-tableViewWidth)/2, 100);
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identify = @"ClassifyViewCell";
    ClassifyViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    return cell;
    
    
}

#pragma mark rightView
-(void)initRightView{
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(tableViewWidth, 0, APPWIDTH - tableViewWidth, APPHEIGHT)];
    [self.view addSubview:rightView];
    
    UIButton *goNext = [BSWidget commonButtonWithFrame:CGRectMake(10, 10, rightView.width-20, 45) title:@"进入分类一频道 >" color:[UIColor orangeColor] fontSize:15.5 target:self action:@selector(goNext)];
    [goNext setBorder:0 cornerRadius:5];
    goNext.backgroundColor = [UIColor colorWithHexString:@"#f8f8f8"];
    [rightView addSubview:goNext];
    
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, goNext.maxY+5, rightView.width, 50)];
    [rightView addSubview:titleView];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, titleView.width / 2, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"#dddddd"];
    line.centerX = titleView.width /2 ;
    line.centerY = titleView.height /2 ;
    [titleView addSubview:line];
    
    UILabel *titleName = [BSWidget commonLableWithFrame:CGRectMake(0, 0, 70, 40) text:@"分类一" color:[UIColor blackColor] font:14];
    titleName.backgroundColor = [UIColor whiteColor];
    titleName.centerX = titleView.width /2 ;
    titleName.textAlignment = NSTextAlignmentCenter ;
    titleName.centerY = titleView.height /2 ;
    [titleView addSubview:titleName];
    
}
-(void)goNext{
    
}
@end
